package com.qys.livesMall.anchor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.anchor.mapper.LiveWatchRecordMapper;
import com.qys.livesMall.anchor.model.entity.LiveWatchRecord;
import com.qys.livesMall.anchor.model.vo.lives.app.LiveWatchRecordVO;
import com.qys.livesMall.anchor.service.AnchorLivesRoomService;
import com.qys.livesMall.anchor.service.LiveWatchRecordService;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 直播观看记录表(LiveWatchRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-09-14 14:47:53
 */
@Service("liveWatchRecordService")
public class LiveWatchRecordServiceImpl extends ServiceImpl<LiveWatchRecordMapper, LiveWatchRecord> implements LiveWatchRecordService {

    @Resource
    private AnchorLivesRoomService livesRoomService;

    @Override
    public Page<LiveWatchRecordVO> selectAll(BaseQuery dto) {

        //当前用户
        dto.setId(String.valueOf(SecurityUtils.getUserId()));

        Page<LiveWatchRecordVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<LiveWatchRecordVO> liveWatchRecords = baseMapper.selectPageByUserId(page, dto);
        for (LiveWatchRecordVO liveWatchRecord : liveWatchRecords) {
            H2AppUser user = livesRoomService.getUserByAnchorId(liveWatchRecord.getAnchorId());
            liveWatchRecord.setAnchorName(user.getNickName());
            liveWatchRecord.setAnchorAvatar(user.getAvatar());
            liveWatchRecord.setUserId(Long.valueOf(user.getId()));
        }
        page.setRecords(liveWatchRecords);
        return page;
    }

    @Override
    public void del(Long id) {
        baseMapper.deleteById(id);
    }
}

